package com.ewanghuiju.app.presenter.login;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.login.PhoneLoginContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.request.LoginRequestBean;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 手机号登录
 * @time 2020/2/14
 */
public class PhoneLoginPresenter extends RxPresenter<PhoneLoginContract.View> implements PhoneLoginContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public PhoneLoginPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void phoneLogin(LoginRequestBean loginRequestBean) {
        addSubscribe(mDataManager.phoneLogin(loginRequestBean)
                        .compose(RxUtil.<MyHttpResponse<LoginResponBean>>rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<LoginResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<LoginResponBean> phoneLoginResponBean) {
                                mView.showLogin(phoneLoginResponBean.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if (e instanceof ApiException) {
//                            mView.showLoginResponseMsg(((ApiException) e).getCode(), e.getMessage());
//                        }
                            }
                        })
        );
    }

    @Override
    public void getUserInfo() {
        addSubscribe(mDataManager.getUserInfo()
                .compose(RxUtil.<MyHttpResponse<UserInfoResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<UserInfoResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<UserInfoResponBean> userInfo) {
                        if (mView != null) {
                            if (userInfo.getIncludeNull() != null) {
                                mDataManager.insertUserInfoResponBean(userInfo.getIncludeNull());
                            }
                            mView.showUserInfo(userInfo.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void wxUserLogin(String unionid) {
        addSubscribe(mDataManager.wxUserLogin(unionid, "")
                .compose(RxUtil.<MyHttpResponse<LoginResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<LoginResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<LoginResponBean> phoneLoginResponBean) {
                        mView.showLogin(phoneLoginResponBean.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }
}
